//
//  fircirnewsdb.m
//  tcpoctest
//
//  Created by apple on 13-4-25.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "fircirnewsdb.h"
#import "FirCirNewsDataSource.h"
#import "FriendStatus.h"

@implementation fircirnewsdb

-(void)creatTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:@"create table if not exists fircirnews(mail text,\
newsid text,time text,differnt char(1),commentnum int,content text)"];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    [[DataControl shareControl].fcnewsDS DelAllNews];
    
    NSString *strsql = [NSString stringWithFormat:@"select * from fircirnews"];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    FMResultSet *s =[db executeQuery:strsql];
    while ([s next]) {
        FriendStatus *fs = [[FriendStatus alloc] init];
        fs.mail = [s stringForColumn:@"mail"];
        fs.statusId = [s stringForColumn:@"newsid"];
        fs.strtime = [s stringForColumn:@"time"];
        fs.stIndex = [s intForColumn:@"differnt"];
        fs.commentsCount = [s intForColumn:@"commentnum"];
        fs.statusText = [s stringForColumn:@"content"];
        
        [[DataControl shareControl].fcnewsDS AddNews:fs];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertNews:(FriendStatus *)fs
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"select * from fircirnews \
where newsid = '%@'"
                ,fs.statusId];

    pthread_mutex_lock(&mutexdatabase);
    
    [db open];

    FMResultSet *s =[db executeQuery:strsql];
    if([s next])
    {
        strsql = [NSString stringWithFormat:@"update fircirnews set \
mail = '%@',time = '%@',differnt = '%d',content = '%@',commentnum = %d where newsid = '%@'"
                  ,fs.mail
                  ,fs.strtime
                  ,fs.stIndex
                  ,fs.statusText
                  ,fs.commentsCount
                  ,fs.statusId];
    }
    else
    {
        strsql = [NSString stringWithFormat:@"insert into fircirnews \
(mail,newsid,time,differnt,commentnum,content) values('%@','%@','%@','%d',%d,'%@')"
                  ,fs.mail
                  ,fs.statusId
                  ,fs.strtime
                  ,fs.stIndex
                  ,fs.commentsCount
                  ,fs.statusText];
    }

    [db executeUpdate:strsql];

    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)delNewsByID:(NSString *)newsid
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM fircirnews WHERE newsid='%@'"
                        ,newsid];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)addCommentCountByID:(NSString *)newid
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"select commentnum from fircirnews \
where newsid = '%@'",newid];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    FMResultSet *s = [db executeQuery:strsql];
    if ([s next]) {
        int icount = [s intForColumn:@"commentnum"];
        strsql = [NSString stringWithFormat:@"update fircirnews set \
commentnum = %d where newsid = '%@'",icount + 1,newid];
        
        [db executeUpdate:strsql];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

@end
